<?php
class User_model extends BZ_Model
{
    protected $table   = 'user';
    protected $table_2 = 'user_account';
    
    public function findByUid($uid)
    {
        $this->db->where('uid', $uid);
        return $this->db->get($this->table);
    }
    
    public function total($params=array())
    {
        if (!empty($params['uid'])) {
            $this->db->where('parent_id', $params['uid']);
        }
        if (!empty($params['user_name'])) {
            $this->db->where('user_name', $params['user_name']);
        }
        if (!empty($params['alias_name'])) {
            $this->db->where('alias_name', $params['alias_name']);
        }
        return $this->db->count_all_results($this->table);
    }
    
    public function page_list($page_num, $num, $params=array())
    {
        if (!empty($params['uid'])) {
            $this->db->where('parent_id', $params['uid']);
        }
        if (!empty($params['user_name'])) {
            $this->db->where('user_name', $params['user_name']);
        }
        if (!empty($params['alias_name'])) {
            $this->db->where('alias_name', $params['alias_name']);
        }
        $this->db->order_by('created_at', 'desc');
        $this->db->limit($page_num, $num);
        return $this->db->get($this->table);
    }
    
    public function findByParam($params=array())
    {
        if (!empty($params['usernames'])) { //必须为数组
            $this->db->where_in('user_name', $params['usernames']);
        }
        if (!empty($params['user_type'])) {
            $this->db->where('user_type & '.$params['user_type'].'=', $params['user_type']);
        }
        return $this->db->get($this->table);
    }
    
    /**
     * 验证当前用户手机号码
     * @param unknown $userName
     */
    public function validateMobilePhone($postData)
    {
        $this->db->where('mobile_phone', $postData['mobile_phone']);
        $this->db->where('uid !=', $postData['uid']);
        return $this->db->get($this->table);
    }
    
    /**
     * 验证用户名称
     * @param unknown $user_name
     */
    public function validateUserName($user_name)
    {
        $this->db->where('user_name', $user_name);
        return $this->db->get($this->table);
    }
    
    /**
     * 更新用户信息
     * @param unknown $postData
     */
    public function updateUserInfo($postData)
    {
        $data = array(
            'alias_name' => trim($postData['alias_name']),
        );
        if (!empty($postData['mobile_phone'])) {
            $data['mobile_phone'] = trim($postData['mobile_phone']);
        }
        $this->db->where('uid', $postData['uid']);
        return $this->db->update($this->table, $data);
    }
    
    public function getUserAccount($params)
    {
        $this->db->select('amount_carry,amount_month,user_type,poundage');
        $this->db->from($this->table.' as user');
        $this->db->join($this->table_2.' as user_account', 'user_account.uid = user.uid', 'inner');
        if (!empty($params['uid'])) {
            $this->db->where('user.uid', $params['uid']);
        }
        return $this->db->get();
    }
    
    public function modifyPassword($postData=array())
    {
        $data = array(
            'pw' => md5($postData['password']),
        );
        $this->db->where('uid', $postData['uid']);
        return $this->db->update($this->table, $data);
    }

    public function findByAliasName($aliasName)
    {
        $this->db->select('uid');
        $this->db->from($this->table);
        $this->db->like('alias_name', $aliasName);
        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 更新 基本信息
     * @param $uid
     * @param $email
     * @return mixed
     */
    public function updateUser($uid, $data)
    {
        $this->db->where('uid', $uid);
        return $this->db->update($this->table, $data);
    }
    
    public function findByParentIds($parentIds = array())
    {
        $this->db->where_in('parent_id', $parentIds['uid']);
        return $this->db->get($this->table);
    }
    
    public function findByIds($data)
    {
        if (!empty($data['uid'])) {
            $this->db->where_in('uid', $data['uid']);
        }
        if (!empty($data['user_name'])) {
            $this->db->where_in('user_name', $data['user_name']);
        }
        if (!empty($data['mobile_phone'])) {
            $this->db->where_in('mobile_phone', $data['mobile_phone']);
        }
        if (!empty($data['user_type'])) {
            $this->db->where('user_type & '.$data['user_type'].'=', $data['user_type']);
        }
        if (!empty($data['parent_id'])) {
            if (is_array($data['parent_id'])) {
                foreach ($data['parent_id'] as $key=>$value) {
                    if ($key == 0) {
                        $this->db->where('parent_id', $value);
                    } else {
                        $this->db->or_where('parent_id', $value);
                    }
                }
            } else {
                $this->db->where('parent_id', $data['parent_id']);
            }
        }
        return $this->db->get($this->table);
    }
}